class Article < ActiveRecord::Base
    validates_presence_of :title, :body
    named_scope :current, :conditions => { :current => true }
    before_create :make_current
    before_save :make_preview #, :process_textile

    def is_current?
        self.created_at.month <= DateTime.now.month  && self.created_at.year <= DateTime.now.year
        #!self.current_month.current_year.nil?
    end
    
    def is_not_current?
        !is_current?
    end
    
    def current
        self if self.is_current?
    end
    
    protected
    
    def process_textile
        textiled = RedCloth.new(self.body)
#        raise textiled
        self.body = textiled.to_html
    end
    
    def make_preview
        self.preview=self.body[0, 300].concat('...')
    end
    
    def make_current
        article = Article.current.first
        if article && article != self
            article.current = false
            article.save()
        end
        self.current = true
    end
end